package com.wz.netty.transport;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandler.Sharable;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

@Sharable
public class TransportServerHandler extends ChannelInboundHandlerAdapter {

	@Override
	public void channelActive(final ChannelHandlerContext ctx) throws Exception {
		System.out.println("Tran-channelActive");
//		ByteBuf m = Unpooled.buffer(10);
//		
//		File f = new File("C://Users//wangzhi//Desktop//gascard-commission.zip");
//		RandomAccessFile randomAccessFile = null;
//		try {
//			randomAccessFile = new RandomAccessFile(f,"rw");
//			while(randomAccessFile.getFilePointer()<randomAccessFile.length()){
//				System.out.println(randomAccessFile.getFilePointer() + "---" + randomAccessFile.length());
//				byte[] fileData = null;
//				int loadLength = (int) (randomAccessFile.length()-randomAccessFile.getFilePointer());
//				if (loadLength<1024){
//					fileData = new byte[loadLength];
//				} else fileData = new byte[1024];
//				randomAccessFile.read(fileData);
//				m.writeBytes(fileData);
//				m.retain();
//				ctx.writeAndFlush(m);
//				m.clear();
//			}
//		} catch (FileNotFoundException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}  catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} finally{
//			try {
//				randomAccessFile.close();
//				m.release();
//			} catch (IOException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//		}
		ctx.channel().disconnect().sync();
//		m.writeBytes(readFile("C://Users//wangzhi//Desktop//pom.xml"));
//		final ChannelFuture future = ctx.writeAndFlush(m);
//		future.addListener(new ChannelFutureListener() {
//			
//			public void operationComplete(ChannelFuture future) throws Exception {
//				ctx.close();
//			}
//		});
	}
	
	@Override
	public void channelInactive(ChannelHandlerContext ctx) throws Exception {
		System.out.println("Tran-channelInactive");
		super.channelInactive(ctx);
	}

	@Override
	public void channelReadComplete(final ChannelHandlerContext ctx) throws Exception {
		System.out.println("Tran-channelReadComplete");
		super.channelReadComplete(ctx);
	}

	@Override
	public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
		System.out.println("Tran-channelRegistered");
		super.channelRegistered(ctx);
	}

	@Override
	public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
		System.out.println("Tran-channelUnregistered");
		super.channelUnregistered(ctx);
	}

	@Override
	public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception {
		System.out.println("Tran-channelWritabilityChanged");
		super.channelWritabilityChanged(ctx);
	}

	@Override
	public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
		System.out.println("Tran-userEventTriggered");
		super.userEventTriggered(ctx, evt);
	}

	@Override
	public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
		System.out.println("Tran-channelRead");
//		ByteBuf m = (ByteBuf) msg;
//		String message = m.toString(Charset.forName("GBK"));
//		System.out.println("С��:" + m.toString(Charset.forName("GBK")));
//		if (message.contains("����")){
//			ByteBuf newbuf = ctx.alloc().buffer(50);
//			newbuf.writeBytes(new String("�Ҳ�ԭ����������յĶ���!").getBytes());
//			ctx.writeAndFlush(newbuf);
//		}
//		ReferenceCountUtil.release(m);
//		super.channelRead(ctx, msg);
	}

	@Override
	public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
		System.out.println("Tran-handlerAdded");
		super.handlerAdded(ctx);
	}

	@Override
	public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
		System.out.println("Tran-handlerRemoved");
		super.handlerRemoved(ctx);
	}

	@Override
	public boolean isSharable() {
		System.out.println("Tran-isSharable");
		return super.isSharable();
	}

	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
		cause.printStackTrace();
		ctx.close();
	}
}
